//
//  StatusViewModel.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/9.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
/// 可重用 cell 标示符
let StatusNormalCellId = "StatusNormaleCellId"
let StatusRetweetedCellId = "StatusRetweetedCellId"

/// 微博视图模型,处理单条微博业务逻辑
class StatusViewModel:CustomStringConvertible {
    /// 微博模型
    var status:Status
    /// 用户头像地址
    var userProfileUrl:NSURL{
        return NSURL(string: self.status.user?.profile_image_url ?? "")!
    }
    /// 用户默认头像
    var userDefaultImage:UIImage {
        return UIImage(named:"avatar_default_big")!
    }
    /// 会员图标
    var userMemberImage:UIImage? {
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")!
        }
        return nil
    }
    /// 用户标识
    var vipIconImage:UIImage? {
    switch status.user?.verified_type ?? -1 {
    case 0: return UIImage(named: "avatar_vip")
    case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
    case 220: return UIImage(named: "avatar_grassroot")
    default: return nil
    }
    }
    /// 缩略图 url数组
    var thumbnailUrls:[NSURL]?
    /// 构造函数
    init(status:Status){
        self.status = status
        
        let strUrls = status.retweeted_status?.pic_urls ?? status.pic_urls
        //创建 url数组
        if strUrls?.count >= 0{
            thumbnailUrls = [NSURL]()
            for dict in strUrls! {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                thumbnailUrls?.append(url!)
            }
        }
        
    }
    /// 缓存行高
    lazy var rowHeight:CGFloat = {
    // 实例化 cell
        var cell : StatusCell
        if self.status.retweeted_status != nil {
        cell = StatusRetweetedCell(style:.Default, reuseIdentifier: StatusRetweetedCellId)
        }else{
        cell = StatusNormalCell(style:.Default, reuseIdentifier: StatusNormalCellId)
        }
        return cell.rowHeight(self)
    }()
    
    /// 描述信息
    var description:String{
        return status.description + "配图数组 \(thumbnailUrls ?? [] as NSArray)"
    }
    /// 被转发原创微博的文字
    var retweetedText: String? {
        
        guard let s = status.retweeted_status else {
            return nil
        }
        
        return "@" + (s.user?.screen_name ?? "") + ":" + (s.text ?? "")
    }
    /// 可重用标示符
    var cellID:String{
        return status.retweeted_status != nil ? StatusRetweetedCellId:StatusNormalCellId
    }
    
    
}